<!DOCTYPE html>
<html>
<head>
    <title>当前登录用户</title>
	<#include "/common/import.ftl" >
    <script type="text/javascript">
        $(function () {
            //获取黑名单冻结列表,解冻只需要将IP设为空
            $('#cacheList').datagrid({
                title: '当前登录用户',
                iconCls: 'icon-ok',
                url: '${ctx}/passport/cache/list!userdata.action',
                nowrap: false,
                striped: true,
                collapsible: false,
                fitColumns: true,
                pagination: true,
                singleSelect: true,
                rownumbers: true,
                remoteSort: false,
                pageList: [15, 20, 30, 50],
                columns: [[
                    {
                        field: 'userEname',
                        title: '登录账户',
                        width: 30,
                        sortable: true,
                        formatter: function (value, rec) {
                            var mt = rec.loginInfo;
                            return mt.userEname;
                        }
                    },
                    {field: 'tokenId', title: 'token值', width: 100, sortable: true},
                    {
                        field: 'ip',
                        title: 'IP地址',
                        width: 40,
                        sortable: true,
                        formatter: function (value, rec) {
                            var mt = rec.loginInfo.lgnLastipaddress;
                            if (mt == "null" || mt == null) {
                                return "其他途径登录";
                            } else {
                                return mt;
                            }
                        }
                    },
                    {
                        field: 'sys',
                        title: '登录系统',
                        width: 40,
                        sortable: true,
                        formatter: function (value, rec) {
                            var mt = rec.loginInfo.sysEname;
                            if (mt == "null" || mt == null) {
                                return "其他途径登录";
                            } else {
                                return mt;
                            }
                        }
                    },
                    {
                        field: 'loginTime',
                        title: '登录时间',
                        width: 40,
                        sortable: true
                    }
                ]],
                toolbar: [{
                    text: '强制退出(慎用)',
                    iconCls: 'icon-remove',
                    handler: function () {
                        deleteCache();
                    }
                }, '-', {
                    text: '全部退出(慎用)',
                    iconCls: 'icon-remove',
                    handler: function () {
                        deleteCacheAll();
                    }
                }, '-', {
                    text: '刷新缓存',
                    iconCls: 'icon-reload',
                    handler: function () {
                        cacheReload();
                    }
                }
                ]
            });
        });

        // 判断是否选中一条记录
        function getSelected() {
            var selected = $('#cacheList').datagrid('getSelected');
            if (selected) {
                return selected;
            } else {
                $.messager.alert('提示', '请选择要操作的数据', 'info');
            }
        }

        // 刷新列表
        function cacheReload() {
            $('#cacheList').datagrid('reload');
        }

        //强制退出
        function deleteCache() {
            var node = getSelected();
            var nodeName = "";
            if (node.loginInfo == null) {
                nodeName = "其他介质登录的用户";
            } else {
                nodeName = node.loginInfo.userEname;
            }
            if (node) {
                $.messager.confirm('确认', '您确定要强制退出<font color="red">' + nodeName + '</font>吗?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "${ctx}/passport/cache/del-user.action?id=" + node.tokenId,
                            dataType: "json",
                            success: function (data) {
                                var result = data;//jQuery.parseJSON(data);
                                if (result.flag == 'T') {
                                    //$.messager.alert('结果', '操作成功', 'info');
                                    var rowIndex = $('#cacheList').datagrid('getRowIndex', node);
                                    $('#cacheList').datagrid('deleteRow', rowIndex);
                                    cacheReload();
                                } else {
                                    $.messager.alert('结果', result.msg, 'info');
                                }
                            },
                            error: function (messg) {
                                $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                            }
                        });
                    }
                });
            }
        }

        //强制退出
        function deleteCacheAll() {
            $.messager.confirm('确认', '您确定要强制全部退出吗?', function (r) {
                if (r) {
                    $.ajax({
                        type: "post",
                        url: "${ctx}/passport/cache/del-user-all.action",
                        dataType: "json",
                        success: function (data) {
                            var result = data;//jQuery.parseJSON(data);
                            if (result.flag == 'T') {
                                cacheReload();
                            } else {
                                $.messager.alert('结果', result.msg, 'info');
                            }
                        },
                        error: function (messg) {
                            $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                        }
                    });
                }
            });
        }
    </script>
</head>
<body>
<table id="cacheList"></table>
</body>
</html>